import Vue from "vue";
import VueRouter from "vue-router";

import Index from '@/components/pages/index/Index';
import About from '@/components/pages/about/About';
import Contact from '@/components/pages/contact/Contact';
import Search from '@/components/pages/search/Search';
import Error from '@/components/pages/error/Error'
import Blog from '@/components/pages/blog/Blog'
import Match from '@/components/pages/match/Match'
import Login from '@/components/pages/login/Login'
import ResetPassword from '@/components/pages/reset-password/ResetPassword'
import Register from '@/components/pages/register/Register'
import Center from '@/components/pages/center/Center'
import BlogManage from '@/components/pages/center/BlogManage'
import UserInfo from '@/components/pages/center/UserInfo'
import SocialManage from '@/components/pages/center/SocialManage'
import TestTopic from '@/components/pages/center/TestTopic'
import SignIn from '@/components/pages/center/SignIn'
import RecourcesManage from '@/components/pages/center/RecourcesManage'
import Editor from '@/components/pages/editor/Editor'
import DepManage from '@/components/pages/center/DepManage'
import BlogDetails from '@/components/pages/blog-details/BlogDetails'
import MatchDetails from '@/components/pages/match-details/MatchDetails'
import TestTopicDetails from '@/components/pages/test-topic-details/TestTopicDetails'

//将router安装到vue中
Vue.use(VueRouter);


const router = new VueRouter({
    routes:[
        {path:'/',redirect:'index'},
        {path:'/index',component:Index},
        {path:'/about',component:About},
        {path:'/contact',component:Contact},
        {path:'/search',component:Search},
        {path:'/error',component:Error},
        {path:'/blog',component:Blog},
        {path:'/match',component:Match},
        {path:'/match/details/:id',component:MatchDetails},
        {path:'/login',component:Login},
        {path:'/reset/password',component:ResetPassword},
        {path:'/register',component:Register},
        {path:'/editor/:id?',component:Editor},
        {path:'/blog/details/:id',component:BlogDetails},
        {path:'/topic/details/:id',component:TestTopicDetails},
        {
            path:'/center',
            component:Center,
            redirect:'/center/user-info',
            children:[
                {path:'sign-in',component:SignIn},
                {path:'blog-manage',component:BlogManage},
                {path:'social-manage',component:SocialManage},
                {path:'user-info',component:UserInfo},
                {path:'test-topic',component:TestTopic},
                {path:'recources-manage',component:RecourcesManage},
                {path:'dep-manage',component:DepManage}
            ]
        }
    ]
})

//解决路径相同报错的问题
const routerPush = VueRouter.prototype.push;
VueRouter.prototype.push=function push(to){
    return routerPush.call(this, to).catch(err => err)
}

function getToken(){

}

// 导航守卫
router.beforeEach(function(to,from,next){
    // console.log('from',from);
    sessionStorage.setItem('lastRoute',from.fullPath);
    // if(to.matched.length==0){
    //     console.log('未匹配到指定路由~')
    //     next('/error');
    // }else{
    //     //路由跳转之后回到顶部
    //     document.documentElement.scrollTop = 0;
    //     next();
    // }
    document.documentElement.scrollTop = 0;
    next();
    
})

//4.向外共享路由实例
export default router;